package com.oceansoft.mobile.biz.admin.controller;

import static com.oceansoft.mobile.common.Constant.GLOBAL_SESSION;
import static org.springframework.web.bind.annotation.RequestMethod.DELETE;
import static org.springframework.web.bind.annotation.RequestMethod.GET;
import static org.springframework.web.bind.annotation.RequestMethod.POST;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.util.HtmlUtils;

import com.oceansoft.mobile.biz.admin.entity.BasesiteArticleNews;
import com.oceansoft.mobile.biz.admin.entity.BasesiteArticlePic;
import com.oceansoft.mobile.biz.admin.entity.BasesiteChannel;
import com.oceansoft.mobile.biz.admin.entity.SysUser;
import com.oceansoft.mobile.biz.admin.service.IBaseSiteAttachmentService;
import com.oceansoft.mobile.biz.admin.service.IBasesiteService;
import com.oceansoft.mobile.common.Constant;
import com.oceansoft.mobile.common.base.entity.FormItem;
import com.oceansoft.mobile.common.base.entity.Pager;
import com.oceansoft.mobile.common.base.entity.Query;
import com.oceansoft.mobile.common.base.entity.Result;

@Controller
@RequestMapping("admin/basesite")
public class BaseSiteAdminController {

	@Resource
    private IBasesiteService basesiteService;

    @Resource
    private IBaseSiteAttachmentService attachmentService;

    @InitBinder
    protected void initBinder(WebDataBinder binder) {
        binder.registerCustomEditor(Date.class, new CustomDateEditor(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"), true));
    }
    

	/**
	 * 进入后台栏目管理首页
	 * 
	 * @return String
	 */
	@RequestMapping(value = "channel", method = GET)
	public String gotoChannelList() {
		return "admin/basesite/channel-list";
	}

	/**
	 * 异步加载客户配置的栏目信息
	 * 
	 * @return Result
	 */
	@ResponseBody
	@RequestMapping(value = "channel/list", method = GET)
	public Pager<BasesiteChannel> loadMerchantChannelsJSON(HttpSession session,
			Query query) {
		SysUser user = (SysUser) session.getAttribute(GLOBAL_SESSION);
		return basesiteService.loadChannelByPage(user.getMerchant_id(), query);
	}

	/**
	 * 进入新增或编辑页面
	 * 
	 * @return String
	 */
	@RequestMapping(value = "channel/new", method = GET)
	public String gotoChannelAdd(HttpSession session,
			@RequestParam(value = "id", required = false) long channelId,
			Model model) {
		if (0 < channelId) {// 编辑
			BasesiteChannel channel = basesiteService.loadChannelById(channelId);
			model.addAttribute("channel", channel);
		} else {
			model.addAttribute("channel", new BasesiteChannel());
		}
		return "admin/basesite/channel-add";
	}

	/**
	 * 新增栏目
	 * 
	 * @return Result
	 */
	@ResponseBody
	@RequestMapping(value = "channel/new", method = POST)
	public Result saveOrUpdatChannel(BasesiteChannel channel, HttpSession session) {
		SysUser user = (SysUser) session.getAttribute(GLOBAL_SESSION);
		channel.setMer_id(user.getMerchant_id());
		if (channel.getType().equals("url")) {
			// 不用做任何处理
		} else if (channel.getType().equals("tel")) {
			channel.setType_val("tel:"+channel.getType_val2());
		} else if (channel.getType().equals("query")) {
			channel.setType_val("");
		}
		return basesiteService.saveOrUpdate(channel);
	}

	/**
	 * 删除栏目信息
	 * 
	 * @return Result
	 */
	@ResponseBody
	@RequestMapping(value = "channel/{channel_id}", method = DELETE)
	public Result deleteChannelById(HttpSession session,
			@PathVariable("channel_id") long channelId) {
		return basesiteService.deleteChannelById(channelId);
	}
	
	
	
	    /**
	     * 进入后台管理列表界面
	     *
	     * @return String
	     */
	    @RequestMapping(value = "articleList", method = GET)
	    public String gotoAdminArticleList(HttpSession session, Model model) {
	    	SysUser user = (SysUser) session.getAttribute(Constant.GLOBAL_SESSION);
	    	 //1.加载当前用户全部栏目信息
	        List<BasesiteChannel> channels = basesiteService.loadChannelByMerchantId(user.getMerchant_id());
	        model.addAttribute("channels", channels);
	        return "admin/basesite/article-list";
	    }
	    

	    /**
	     * 进入新增或编辑页面
	     *
	     * @return String
	     */
	    @RequestMapping(value = "article/new", method = GET)
	    public String gotoArticleEdit(HttpSession session, @RequestParam(value = "id", required = false) long sid, Model model) {
	    	SysUser user = (SysUser) session.getAttribute(Constant.GLOBAL_SESSION);
	        //1.加载当前用户全部栏目信息
	        List<BasesiteChannel> channels = basesiteService.loadChannelByMerchantId(user.getMerchant_id());
	        model.addAttribute("channels", channels);
	        if (0 < sid) {//编辑
	        	BasesiteArticleNews art = basesiteService.loadArticleById(sid);
	        	art.setContent(HtmlUtils.htmlEscape(art.getContent()));
	            //加载线路图片信息
	            List<BasesiteArticlePic> pics = attachmentService.loadProductPics(sid);
	            StringBuilder sb = new StringBuilder();
	            for (BasesiteArticlePic pic : pics) {
	                sb.append(pic.getId()).append(",");
	            }
	            model.addAttribute("pictures", pics);
	            model.addAttribute("pictureIds", sb.toString());
	            model.addAttribute("art", art);
	        } else {
	            model.addAttribute("art", new BasesiteArticleNews());
	        }
	        return "admin/basesite/article-add";
	    }

	    /**
	     * 表单提交
	     *
	     * @return Result
	     */
	    @ResponseBody
	    @RequestMapping(value = "article/new", method = POST)
	    public Result submitProductForm(BasesiteArticleNews art, HttpSession session) {
	    	SysUser user = (SysUser) session.getAttribute(Constant.GLOBAL_SESSION);
	        art.setMer_id(user.getMerchant_id());
	        art.setUpdateTime(new Date());
	        art.setStatus(1);
	        return basesiteService.saveOrUpdate(art);
	    }

	    /**
	     * 后台分页加载内容信息
	     *
	     * @return Result
	     */
	    @ResponseBody
	    @RequestMapping(value = "article/list", method = GET)
	    public Pager<BasesiteArticleNews> loadArticleJSON(HttpSession session, Query query) {
	    	SysUser user=(SysUser) session.getAttribute(Constant.GLOBAL_SESSION);
	        return basesiteService.loadArticleNewsByPage(user.getMerchant_id(), query);
	    }

	    /**
	     * 删除文章信息
	     *
	     * @return Result
	     */
	    @ResponseBody
	    @RequestMapping(value = "article/{article_id}", method = DELETE)
	    public Result deleteArticleById(HttpSession session, @PathVariable("article_id") long sid) {
	        return basesiteService.deleteArticleById(sid);
	    }
	    
	    
	    /**
	     * 产品列属性编辑
	     *
	     * @param orderId  订单编号
	     * @param fromItem 表单对象
	     * @return Result
	     */
	    @RequestMapping(value = "article/property/{id}", method = POST)
	    @ResponseBody
	    public Result changeArticlePropertyVal(@PathVariable("id") long pid, FormItem fromItem) {
	        return basesiteService.updateColunmByName(pid, fromItem.getProp(), fromItem.getVal());
	    }

}
